<template>
	<view class="content">
		<view v-if="!show">
			<!-- <u-navbar title="登录" :autoBack="true" :placeholder="true"></u-navbar> -->
			<image src="../../static/login.png"
				style="width: 289rpx;height: 326rpx;margin: 0 auto;margin-top: 180rpx;display: block;">
			</image>
<!-- 			<button v-if="!userId" class="u-reset-button btn1" @click="login" :loading="loading">确认授权</button>
			<button v-if="userId" class="u-reset-button btn1" open-type="getPhoneNumber" @getphonenumber="getphone"
				:loading="loading">授权手机号</button> -->
				<button class="u-reset-button btn1" open-type="getPhoneNumber" @getphonenumber="getphone"
					:loading="loading">授权手机号</button>
		</view>
		<view>
			<u-modal v-model="show" @confirm="close()" title="用户协议" title-style="fontSize:16rpx;fontW">
				<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-Y" style="height: 937rpx;">
					<view class="slot-content">
						<rich-text :nodes="content"></rich-text>
					</view>
				</scroll-view>
			</u-modal>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loading: false,
				userId: false,
				code: '',
				show: true, //用户协议,true默认显示
				content: '', //用户协议内容
				scrollTop: 0,
			}
		},
		onShow() {
			// console.log(uni.getStorageSync('userId'), '--')
			// this.init()
			this.getCode()
			this.getyhxy()
		},
		methods: {
			init() {
				if (this.show == false) {
					if (uni.getStorageSync('userId')) {
						this.userId = true
					} else {
						this.userId = false
					}
					// console.log(this.userId)
					this.getCode()
				}
			},
			//获取用于协议内容
			getyhxy() {
				uni.$u.http.post('index/user_agreement', {}).then(res => {
					this.content = res.data.info
					this.show = true
				})
			},
			//关闭用户协议
			close() {
				this.show = false
				// console.log('close');
				this.init()
			},
			// 获取code
			getCode() {
				let that = this
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						that.code = loginRes.code
						// that.$u.post('user/postopenid',{
						// 	code:loginRes.code
						// }).then(res=>{
						// 	uni.setStorageSync('openid',res.data.openid)
						// })
					},
				})
			},
			// 登录小程序
			logins() {
				
				let that = this
				uni.$u.http.post('user/Userauthorization', {
					openid: uni.getStorageSync('openid'),
					// nickname: data.nickName,
					// user_nickname: data.nickName,
					// avatar: data.avatarUrl,
					// sex: data.gender,
					// headimgurl: data.avatarUrl,
					// scene: uni.getStorageSync('scene')
				}).then(res => {
					uni.setStorageSync('userId', res.data.user_id)
					uni.showToast({
						icon: 'success',
						title: '授权成功'
					})
					this.userId = true
					that.loading = false

				})
			},
			login() {
				var that = this
				this.loading = true
				that.logins()
				// wx.getUserProfile({
				// 	desc: "获取你的昵称、头像、地区及性别",
				// 	success: res => {
				// 		console.log(res)
				// 		let wxUserInfo = res.userInfo;
				// 		that.logins(wxUserInfo)
				// 	},
				// 	fail: res => {
				// 		//拒绝授权
				// 		uni.showToast({
				// 			icon: 'none',
				// 			title: '取消授权'
				// 		})
				// 		that.loading = false
				// 		return;
				// 	}
				// })

			},
			getphone(e) {
				if (!e.detail.iv) {
					uni.showToast({
						title: '获取手机号失败',
						icon: 'none'
					})
					return;
				}
				this.loading = true
				var that = this;
				//检查登录是否过期
				uni.checkSession({
					success(val) {
						if (val.errMsg == 'checkSession:ok') {
							// console.log(111)
							var obj = {
								code: that.code,
								iv: e.detail.iv,
								encryptedData: e.detail.encryptedData
							}
							that.decryptPhone(obj);
						} else {
							// console.log(222)
							uni.login({
								provider: 'weixin',
								success(res) {
									let code = res.code;
									var obj = {
										code,
										iv: e.detail.iv,
										encryptedData: e.detail.encryptedData
									}
									that.decryptPhone(obj);
								}
							})
						}
					}
				})
			},
			decryptPhone(obj) {
				uni.$u.http.post('user/getMobile', obj).then(res => {
					// console.log(res.data)
					this.postPhone(res.data.phoneNumber)
					// this.postPhone()
				})
			},
			// 提交手机号
			postPhone(phone) {

				uni.$u.http.post('user/binding', {
					mobile: phone,
					// openid: uni.getStorageSync('openid')
				}).then(res => {
					console.log(res, '授权  ')

					uni.setStorageSync('phoneNumber', phone)
					uni.showToast({
						icon: 'success',
						title: '授权成功'
					})
					this.loading = false
					uni.$emit('updates', {
						type:1
					})
					uni.navigateBack({
						delta: 1
					})
					uni.$emit('a1', {
						a1: '1'
					})

				}).catch(err => {
					uni.showToast({
						icon: 'none',
						title: err.msg
					})
				})
			}
		}
	}
</script>

<style>
	.content {
		height: 100vh;
		background: #FFFFFF;
		margin: 10rpx 20rpx;
		border-radius: 20rpx;
		display: flex;
		flex-direction: column;
		/* justify-content: center; */
		/* align-items: center; */
		/* justify-content: center; */
	}

	.btn1 {
		width: 650rpx;
		height: 80rpx;
		background: #FBAA27;
		border-radius: 40rpx;
		color: #FFFFFF;
		font-size: 36rpx;
		font-weight: bold;
		line-height: 80rpx;
		margin-top: 200rpx;
	}

	.slot-content {
		margin-top: 17rpx;
		padding: 20rpx 25rpx;
		/* height: 937rpx;
		overflow: auto; */
	}
</style>
